<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Photo</title>
<link href="<?php echo(Yii::app()->params['app']['host'])?>/statics/web/css/style.css?v=<?php echo(Yii::app()->params['app']['version'])?>" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<link href="<?php echo(Yii::app()->params['app']['host'])?>/statics/web/css/frame_android_down.css?v=<?php echo(Yii::app()->params['app']['version'])?>" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="<?php echo(Yii::app()->params['app']['host'])?>/statics/web/js/infownd.js?v=<?php echo(Yii::app()->params['app']['version'])?>" charset="utf-8"></script>
<script type="text/javascript" src="<?php echo(Yii::app()->params['app']['host'])?>/statics/web/js/map.js?v=<?php echo(Yii::app()->params['app']['version'])?>" charset="utf-8"></script>
<?php
    $helpLink = "http://bbs.miniyun.cn/forum.php?mod=viewthread&tid=176";
    $exifLink = "http://bbs.miniyun.cn/forum.php?mod=viewthread&tid=176";
    $androidClick = "onclick=\"javascript:document.getElementById(\'android_down_pop\').style.visibility =\'visible\';\"";
    $iosClick = "onclick=\"javascript:document.getElementById(\'ios_down_pop\').style.visibility =\'visible\';\"";
    $ios_href     = "http://www.miniyun.cn/index.php?r=app/download&mid=".Yii::app()->params['app']['mid']."&type=ios";
?>
<script type="text/javascript">
var map;
var timer = null;
var clusters = [];
var markers = null;
var address;
var NoRightSide = false;
function initialize() {
   myLatlng= new google.maps.LatLng(31.2, 95);
    var myOptions = {
        zoom : 4,
        center : new google.maps.LatLng(32, 108.8),
        mapTypeId : google.maps.MapTypeId.ROADMAP,
        navigationControlOptions:{style:google.maps.NavigationControlStyle.SMALL}
    };
    map = new google.maps.Map(document.getElementById('map_canvas'), myOptions);
    google.maps.event.addListener(map, 'zoom_changed',  
            function() {  
                if (map.getZoom() < 3) {
                    map.setZoom(3);
                }else if(map.getZoom() > 18){
                    map.setZoom(18);
                }
                closeInfoWnd();
    });  
    google.maps.event.addListener(map, 'click', closeInfoWnd);
    google.maps.event.addListener(map, 'bounds_changed', onMapChange);

    showDialog(myOptions);
}

function showDialog(myOptions) {
    <?php if($flag == 0){ ?>
        var contentString = '<div class="map_bg"><p>'
                          +'<b style="color: white;font-size: 14px;">尊敬的用户，您的账号还无GPS信息的照片</b>'
                          +'<br /> 我们将把iOS照片地点的体验呈现给您，'
                          +'<br />用手机拍照并上传试试！</p><ul>'
                          <?php if($is_mini){ ?>  //定制版的时候 不显示帮助手册 
                          +'<li class="help_i"><a href="<?php echo $exifLink; ?>" target="_blank">帮助手册</a></li>'
                          +'<li class="Android_i">' +'<a href="#" <?php echo $androidClick ?> >Android下载</a></li>'
                          +'<li class="iphone_i"><a href="#" <?php echo $iosClick; ?>>iPhone下载</a></li></ul></div>';
                          <?php }else{ ?>
                          +'<li class="Android_i">' +'<a href="#" <?php echo $androidClick ?> >Android下载</a></li>'
                          +'<li class="iphone_i"><a href="<?php echo $ios_href; ?>">iPhone下载</a></li></ul></div>';
                          <?php }?>
                          
             var marker = new google.maps.Marker({    position: myLatlng, map: map,    title:"提示信息"});
             var infoBox = new MyInfoBox({latlng:myLatlng, map: map, content: contentString});
     <?php }?>
     <?php if($flag == -1){ ?>
        var contentString = '<div class="map_bg"><p>'
                           +'<b style="color: white;font-size: 14px;">尊敬的用户，您的系统还没有安装file_exif扩展，</br>请安装后再试 </b>'
                          
                           <?php if($is_mini){ ?>  //定制版的时候 不显示帮助手册 
                           +'<li class="help_i"><a href="<?php echo $exifLink; ?>" target="_blank">帮助手册</a></li>'
                           <?php }?>
                           +'</ul></div>';
              var marker = new google.maps.Marker({    position: myOptions.center,    map: map,    title:"提示信息"});
              var infoBox = new MyInfoBox({latlng: myOptions.center, map: map, content: contentString});
      <?php }?>
}

$(function(){
    initialize();
});


function onMapChange() {
    clearClusters();
    
    if( ! timer ){
        timer = setTimeout('getPhotos()', 1000);
    }
}

function getPhotos() {
    
    var bounds = map.getBounds();
    var ne = bounds.getNorthEast();
    var sw = bounds.getSouthWest();

    var ne_lat = ne.lat();
    var ne_lng = ne.lng();
    var sw_lat = sw.lat();
    var sw_lng = sw.lng();
    var data = 'ne_lat='+ne_lat+'&ne_lng='+ne_lng+'&sw_lat='+sw_lat+'&sw_lng='+sw_lng;
    
    $.ajax({
        type : "POST",
        url : "<?php echo(Yii::app()->params['app']['host']); ?>/index.php?r=netdisk/getphotos&t="+new Date().getTime(),
        data : data,
        dataType : 'json',
        success : onGetPhotos
    });
}

function clearClusters(){
    if(markers == null){
        return null;
    }
    for(var i=0; i<markers.length; i++){
        if(markers[i].infoWnd){
            markers[i].infoWnd.setMap(null);
        }
        
        markers[i].setMap(null);
    }
    markers  = [];
    clusters = [];
}

function onGetPhotos(jsonRet){

    clearTimeout(timer);
    timer = null;
    
    var photos = jsonRet;
    for(var i=0, photo; photo = photos[i]; i++){
        var distance = 1000000000;
        var clusterToAddTo = null;
        for(var j=0, cluster; cluster = clusters[j]; j++){
            var center = cluster.center;
            var d = getDistance({lat:center.lat, lng:center.lng}, {lat:photo.lat, lng:photo.lng});
            
            if(d < distance){
                clusterToAddTo = cluster;
                distance = d;
            }
        }
        
        if(clusterToAddTo && isPhotoInCluster(clusterToAddTo, photo)){
            addPhotoToCluster(clusterToAddTo, photo);
        }else{
            var cluster = {};
            cluster.center = {lat:0, lng:0};
            cluster.photos = [];
            addPhotoToCluster(cluster, photo);
            clusters.push(cluster);
        }
    }
    
    drawCluster();
}

function drawCluster(){
    markers = [];
    var zoom = map.getZoom();
    for(var i=0, cluster; cluster = clusters[i]; i++){
        var marker = new google.maps.Marker({
            position: new google.maps.LatLng(cluster.center.lat, cluster.center.lng),
            icon: "<?php echo(Yii::app()->params['app']['host']); ?>/statics/web/images/point.png",
            map: map
        });
        
        marker.cluster = cluster;
        marker.open = false;
        marker.index = i;
        
        google.maps.event.addListener(marker, 'click', function(){
            openInfoWnd(this);
        });
        
        markers.push(marker);
    }
}
InfoBox.prototype.createElement = function() {
    var panes = this.getPanes();
    var div = this.div_;
    if (!div) {
        div = this.div_ = document.createElement("div");
        div.style.border = "0px none";
        div.style.position = "absolute";
        div.style.background = "url('<?php echo(Yii::app()->params['app']['host']); ?>/statics/web/images/infownd.png')";
        div.style.width = this.width_ + "px";
        div.style.height = this.height_ + "px";
        var contentDiv = document.createElement("div");
        contentDiv.innerHTML = this.content_;
        function removeInfoBox(ib) {
            return function() {
                ib.setMap(null);
            };
        }
        div.appendChild(contentDiv);
        div.style.display = 'none';
        panes.floatPane.appendChild(div);
    } else if (div.parentNode != panes.floatPane) {
        div.parentNode.removeChild(div);
        panes.floatPane.appendChild(div);
    } else {
    }
}
MyInfoBox.prototype.createElement = function() {
    var panes = this.getPanes();
    var div = this.div_;
    if (!div) {
        div = this.div_ = document.createElement("div");
        div.style.border = "0px none";
        div.style.position = "absolute";
        //div.style.background = "url('<?php echo(Yii::app()->params['app']['host']); ?>/statics/web/images/infownd.png')";
        div.style.width = this.width_ + "px";
        div.style.height = this.height_ + "px";
        var contentDiv = document.createElement("div");
        contentDiv.innerHTML = this.content_;
        function removeInfoBox(ib) {
            return function() {
                ib.setMap(null);
            };
        }
        div.appendChild(contentDiv);
        div.style.display = 'none';
        panes.floatPane.appendChild(div);
    } else if (div.parentNode != panes.floatPane) {
        div.parentNode.removeChild(div);
        panes.floatPane.appendChild(div);
    } else {
    }
}
function openInfoWnd(marker){
    var cluster = marker.cluster;
    //
    // 获取地理信息位置
    //
    address = getAddress(cluster.center.lat, cluster.center.lng);
    
    if(marker.open){
        return;
    }
    
    closeInfoWnd();
    
    marker.open = true;
    
    if(marker.infoWnd){
        marker.infoWnd.setMap(map);
        return;
    }
    
    var i = marker.index;
    var content = '<div class="infobox_content">'
         +'<img class="thumbnail" src="<?php echo(Yii::app()->params['app']['host']); ?>/index.php?r=thumbnail/index&size=normal&tid='+cluster.photos[0].id+'&ver='+cluster.photos[0].version_id+'"/>'
         +'<span class="photonum">'+cluster.photos.length+'张照片</span>'
         +'<a class="viewlink" href="javascript:viewPhotos('+i+')"></a>'
          +'</div>';
    var infoBox = new InfoBox({latlng: marker.getPosition(), map: map, content: content});
    marker.infoWnd = infoBox;
}

function closeInfoWnd(){
    if(markers){
        for(var i=0; i<markers.length; i++){
            if(markers[i].open && markers[i].infoWnd){
                markers[i].open = false;
                markers[i].infoWnd.setMap(null);
            }
        }
    }
}

function rad(d){
    return d * Math.PI / 180.0;
}

function getDistance(p1, p2){
    var lat1 = p1.lat;
    var lng1 = p1.lng;
    var lat2 = p2.lat;
    var lng2 = p2.lng;
    var radLat1 = rad(lat1);
    var radLat2 = rad(lat2);
    var a = radLat1 - radLat2;
    var b = rad(lng1) - rad(lng2);
    var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2)+Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));
    s = s * 6378137;
    s = Math.round(s * 10000) / 10000;
    return s;
}

function addPhotoToCluster(cluster, photo){
    cluster.photos.push(photo);
    var len = cluster.photos.length;
    if(len==1) {
        cluster.center.lat = photo.lat;
        cluster.center.lng = photo.lng;
        return;
    }
    var lat, lng;
    
    if(Math.abs(cluster.center.lng - photo.lng)>180){
        cluster.center.lng < 0 && (cluster.center.lng += 360);
        photo.lng < 0 && (photo.lng += 360);
    }
    lng = (cluster.center.lng*(len-1)+parseFloat(photo.lng))/len;
    lat = (cluster.center.lat*(len-1)+parseFloat(photo.lat))/len;
    
    lng > 180 && (lng -= 360);
    cluster.center.lat = lat;
    cluster.center.lng = lng;
}

function isPhotoInCluster(cluster, photo){
    var center = cluster.center;
    var d = getDistance({lat:center.lat, lng:center.lng}, {lat:photo.lat, lng:photo.lng});
    
    var zoom = map.getZoom();
    
    var dst;
    switch(zoom){
    case 3:
        dst = 200000;
        break;
    case 4:
        dst = 100000;
        break;
    case 5:
        dst = 80000;
        break;
    case 6:
        dst = 60000;
        break;
    case 7:
        dst = 40000;
        break;
    case 8:
        dst = 20000;
        break;
    case 9:
        dst = 10000;
        break;
    case 10:
        dst = 5000;
        break;
    case 11:
        dst = 2000;
        break;
    case 12:
        dst = 1000;
        break;
    case 13:
        dst = 800;
        break;
    case 14:
        dst = 600;
        break;
    case 15:
        dst = 400;
        break;
    case 16:
        dst = 200;
        break;
    case 17:
        dst = 50;
        break;
    case 18:
        dst = 10;
    }
    
    return d <= dst;
}

function getAddress(lat,lng){
    
    var locat = new google.maps.LatLng(lat,lng); 
    var geocoder = new google.maps.Geocoder(); 
    geocoder.geocode({'latLng': locat}, function(results, status) { 
        //异步解析gps地址 
        if (status == google.maps.GeocoderStatus.OK) { 
            if (results[2]) { 
                address = results[2].formatted_address;
                address = address.replace("中国", "");
            }
        }
    }); 
    return address;
}

function viewPhotos(i){
    var cluster = clusters[i];
    var tr_lat = bl_lat = cluster.photos[0].lat;
    var tr_lng = bl_lng = cluster.photos[0].lng;
    var ids    = "";
    for(var i=0, photo; photo=cluster.photos[i]; i++){
        var lat = photo.lat;
        var lng = photo.lng;
        
        if(Math.abs(tr_lng-lng)>180){
            lng < 0 && (lng += 360);
            tr_lng < 0 && (tr_lng += 360);
        }
        
        lng > tr_lng && (tr_lng = lng);
        lng < bl_lng && (bl_lng = lng);
        
        lng > 180 && (lng -= 360);
        
        lat > tr_lat && (tr_lat = lat);
        lat < bl_lat && (bl_lat = lat);

        ids += "," + photo.version_id;
    }
    ids = ids.substring(1);
    loc = address;
    parent.parent.window.PageCTL
    .GOTO_URL("<?php echo(Yii::app()->params['app']['host']); ?>/index.php?r=List/index&aid=35&flag=-1&loc=" + loc + "&ids=" + ids);
    
}
</script>
</head>
<body style="height: 660px;">
<div id="map_canvas"  style="width: 100%; height: 100%; margin: 0px auto; border: 1px solid #ccc;">
</div>
<?php if($flag == -1){ ?>
<!--显示没有配置exif模块-->
<?php include dirname(__FILE__).'/../site/downmanager.php';?>
<!--显示没有配置exif模块-->
<?php }else if($flag == 0){?>
<!--显示没有GPS信息的照片-->
<?php include dirname(__FILE__).'/../site/downmanager.php';?>
<!--显示没有GPS信息的照片-->
<?php } ?>
</body></html>